﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KocAllianceLister
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainWindowViewModel _MWVM = null;
        AllianceMembers _wndAllianceMembers = null;
        AllianceComparer _wndAllianceComparer = null;
        AllianceComparerViewModel _ACVM = null;

        public MainWindow()
        {
            InitializeComponent();

            _MWVM = new MainWindowViewModel();
            DataContext = _MWVM;
        }

        private void lvAllyList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if(lvAllyList.SelectedItem != null)
            {
                KocAllianceListItem KALI = lvAllyList.SelectedItem as KocAllianceListItem;
                if(KALI != null)
                {
                    Int32 intAllianceId = -1;
                    Int32.TryParse(KALI.AllianceId, out intAllianceId);

                    if (intAllianceId != -1)
                    {
                        Cursor = Cursors.Wait;

                        if (_wndAllianceMembers == null)
                        {
                            _wndAllianceMembers = new AllianceMembers(_MWVM.EC, _MWVM.UH, intAllianceId, KALI.Name, _MWVM.ExecuteViewCourt);
                        }

                        _wndAllianceMembers.ShowDialog();

                        _wndAllianceMembers = null;

                        Cursor = Cursors.Arrow;
                    }
                }
            }
        }

        private void mnuAddToCompareList_Click(object sender, RoutedEventArgs e)
        {
            if (lvAllyList.SelectedItems.Count > 0)
            {
                _ShowAllianceComparer();
                
                foreach (KocAllianceListItem KALI in lvAllyList.SelectedItems)
                {
                    Boolean Exists = false;

                    foreach (AllianceComparerListItem acliSeek in _ACVM.AllianceCompareList)
                    {
                        if (acliSeek.AllianceId == KALI.AllianceId)
                        {
                            Exists = true;
                            break;
                        }
                    }

                    if (!Exists)
                    {
                        AllianceComparerListItem ACLI = new AllianceComparerListItem();
                        ACLI.AllianceId = KALI.AllianceId;
                        ACLI.AllianceName = KALI.Name;
                        _ACVM.AllianceCompareList.Add(ACLI);
                    }
                }
            }
        }

        private void mnuRemoveFromCompareList_Click(object sender, RoutedEventArgs e)
        {
            if (lvAllyList.SelectedItems.Count > 0)
            {
                _ShowAllianceComparer();

                foreach (KocAllianceListItem KALI in lvAllyList.SelectedItems)
                {
                    foreach(AllianceComparerListItem acliSeek in _ACVM.AllianceCompareList)
                    {
                        if(acliSeek.AllianceId == KALI.AllianceId)
                        {
                            _ACVM.AllianceCompareList.Remove(acliSeek);
                            break;
                        }
                    }
                }
            }
        }

        private void _ShowAllianceComparer()
        {
            if (_wndAllianceComparer == null)
            {
                _wndAllianceComparer = new AllianceComparer(_MWVM.EC, _MWVM.UH, _MWVM.ExecuteViewCourt);
                _wndAllianceComparer.Unloaded += new RoutedEventHandler(_wndAllianceComparer_Unloaded);
            }

            if (_ACVM == null)
            {
                _ACVM = _wndAllianceComparer.DataContext as AllianceComparerViewModel;
            }
            
            if (_ACVM != null)
            {
                _wndAllianceComparer.Show();
            }
        }

        void _wndAllianceComparer_Unloaded(object sender, RoutedEventArgs e)
        {
            _wndAllianceComparer.Unloaded -= _wndAllianceComparer_Unloaded;
            _wndAllianceComparer = null;
            _ACVM = null;
        }
    }
}
